﻿using Domin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using JwT;
using Newtonsoft.Json;
using Snowflake.Core; 

namespace App
{
    public class Apps : IRApp
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private readonly IRDomin<Peoples> db;
        private readonly IRDomin<Roles> dbs;
        private readonly IRDomin<Powers> dbp;
        private readonly IRDomin<RolePowers> dbw;
        private readonly IRDomin<Mune> dbm;
        private readonly IRDomin<RoleMune> dbr;
        private readonly IRDomin<Users> dbu;
        private readonly ITokenHelper tokenHelper;

        public Apps(IRDomin<Peoples> db, IRDomin<Roles> dbs, IRDomin<Powers> dbp, IRDomin<RolePowers> dbw, IRDomin<Mune> dbm, IRDomin<RoleMune> dbr, IRDomin<Users> dbu, ITokenHelper tokenHelper)
        {
            this.db = db;
            this.dbs = dbs;
            this.dbp = dbp;
            this.dbw = dbw;
            this.dbm = dbm;
            this.dbr = dbr;
            this.dbu = dbu;
            this.tokenHelper = tokenHelper;
        }

        public int Add(Peoples peoples)
        {
            db.Add(peoples);
            return db.Ser();
        }

        public int Del(string id)
        {
            db.Del(id);
            return db.Ser();
        }

        public int HAdd(string nm, int id)
        {
            db.HAdd(nm, id);
            return db.Ser();
        }

        public List<Mune> MShow()
        {
            var uList = dbm.Show().Where(x => x.EId == 0).ToList();
            ChildrenMshow(uList);
            return uList;
        }
        private void ChildrenMshow(List<Mune> mm)
        {
            foreach (var item in mm)
            {
                item.mList = new List<Mune>();
                item.mList = dbm.Show().Where(x => x.EId == item.MId).ToList();
                ChildrenMshow(item.mList);
            }
        }

        public List<RolePowers> OShow()
        {
            return dbw.Show().Include("Roles").Include("Powers").ToList();
        }

        public List<Peoples> PShow()
        {
            return db.Show().Include("Roles").ToList();
        }


        public int RAdd(RolePowers rolePowers)
        {
            dbw.Add(rolePowers);
            return dbw.Ser();
        }

        public List<Roles> RShow()
        {
            return dbs.Show().ToList();
        }

        public List<Powers> WShow()
        {
            var rList = dbp.Show().Where(x => x.KId == 0).ToList();
            ChildrenWshow(rList);
            return rList;
        }
        private void ChildrenWshow(List<Powers> pp)
        {
            foreach (var item in pp)
            {
                item.WList = new List<Powers>();
                item.WList = dbp.Show().Where(x=>x.KId==item.WId).ToList();
                ChildrenWshow(item.WList);
            }
        }

        public int MAdd(RoleMune roleMune)
        {

            dbr.Add(roleMune);
            return dbr.Ser();
        }

        public List<RoleMune> UShow()
        {
            return dbr.Show().Include("Roles").Include("Mune").ToList();
        }

        public ResponseModel Login(UserLoginDto user)
        {
            return Login(user, 60);
        }

        public ResponseModel Login(UserLoginDto user, int v)
        {
            //定义一个响应信息的对象
            var res = new ResponseModel();
            //获取用户表中的所有数据进行用户名称和密码的查询检查用户是否已存在
            Users users = dbu.Show().Where(x => x.LoginName.Equals(user.LoginName) && x.Pwd.Equals(user.Pwd)).FirstOrDefault();
            //判断是否有该用户
            if (users != null)
            {
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>
                {
                    { "LoginName",users.LoginName},
                };

                //提示数字
                res.Code = 200;
                //提示信息
                res.Msg = "登录成功";
                //数据
                res.Data = users;
                //帮助类来生成JWT字符串，JWT字符串返回给浏览器
                res.TokenInfo = tokenHelper.CreateToken(keyValuePairs);
                //引用雪花ID
                var worker = new IdWorker(1, 1);
                var KId = "";
                KId = worker.NextId().ToString();
                string json = JsonConvert.SerializeObject(res.TokenInfo);
                RedisHelper.Set(KId, json,5*60);
                return res;
            }
            else
            {
                res.Code = 401;
                res.Msg = "用户名或密码不正确";
                return res;
            }
        }
    }
}
